// 2018/09/23 ver. 2.1 Eliminate backlash & Power down correction #include "Stepper.h" #define STEPS 32 // Number of steps for one revolution of Internal shaft // 2048 steps for one revolution of External shaft volatile boolean TurnDetected; // need volatile for Interrupts volatile boolean rotationdirection; // CW or CCW rotation const int PinCLK=2; // Generating interrupts using CLK signal const int PinDT=3; // Reading DT signal const int PinSW=4; // Reading Push Button switch const int PinHiLoSW=5; // Reading Speed(Step) Hi/Lo switch int Speed; // Step per one click int Speedswposition=0; int RotaryPosition=0; // To store Stepper Motor Position int PrevPosition; // Previous Rotary position Value to check accuracy int StepsToTake; // How much to move Stepper int lastDirection = 0; // 0:Neutral (initial), 1: CW, 2: CCW // Setup of proper sequencing for Motor Driver Pins In1, In2, In3, In4 in the // sequence 1-3-2-4, Connection order when "Stepper.h" is selected for 28BYJ-48 motor Stepper small_stepper(STEPS, 8, 10, 9, 11); // Interrupt routine runs if CLK goes from HIGH to LOW void isr () { delay(1); // delay for Debouncing if (digitalRead(PinCLK)) rotationdirection= digitalRead(PinDT); else rotationdirection= !digitalRead(PinDT); TurnDetected = true; } void setup () { pinMode(PinCLK,INPUT); //digitalWrite(PinCLK, HIGH); // pull-up resistor Check the encoder PCB(PWB)! // Comment out this line, if pull-up resistor exist pinMode(PinDT,INPUT); //digitalWrite(PinDT, HIGH); // Same as above pinMode(PinSW,INPUT); digitalWrite(PinSW, HIGH); // Same as above pinMode(PinHiLoSW,INPUT); digitalWrite(PinHiLoSW, HIGH); // Pull-Up resistor for step select switch attachInterrupt (0,isr,FALLING);// interrupt 0 always connected to pin 2 } void loop () { if (!(digitalRead(PinHiLoSW))) // When PinHiLoSW is GND, step is large. { Speed=9; // "9" is 1.582deg per one click ( 9 x 0.1758deg) } else { Speed=1; // "1" is 0.1758deg per one click } small_stepper.setSpeed(600); if (!(digitalRead(PinSW))) // check if button is pressed { if (RotaryPosition == 0) // check if button was already pressed { } else { small_stepper.step(-(RotaryPosition*Speed)); RotaryPosition=0; // Reset position to ZERO } } // Runs if rotation was detected if (TurnDetected) { PrevPosition = RotaryPosition; // save previous position in variable if (rotationdirection) { RotaryPosition=RotaryPosition-1; // decrase Position by 1 } else { RotaryPosition=RotaryPosition+1; // increase Position by 1 } TurnDetected = false; // do NOT repeat IF loop until new rotation detected // Which direction to move Stepper motor if ((PrevPosition + 1) == RotaryPosition) // Move motor CW { if (lastDirection == 2) // Last direction was CCW { StepsToTake = Speed+5; // Backlash removal(backlash is 4~7 steps) } else { StepsToTake=Speed; } lastDirection = 1; small_stepper.step(StepsToTake); } if ((RotaryPosition + 1) == PrevPosition) // Move motor CCW { if (lastDirection == 1) { StepsToTake = -(Speed+5); // Backlash removal(backlash is 4~7 steps) } else { StepsToTake=-Speed; } lastDirection = 2; small_stepper.step(StepsToTake); } } else // Power consumption reduction When rotary sw stop, turn off the motor // after adequate delay time. { delay(30); digitalWrite(8, LOW); digitalWrite(9, LOW ); digitalWrite(10, LOW); digitalWrite(11, LOW); } }